home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Macintosh Sample Code / SC.014.CPlusTESample / Document.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-18  |  7.5 KB  |  246 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0
  4.     File:        Document.h
  5.     Uses:       Application.h
  6.                 List.h
  7.  
  8.     by Andrew Shebanow
  9.     of Apple Macintosh Developer Technical Support
  10.  
  11.     Copyright © 1989-1990 Apple Computer, Inc.
  12.     All rights reserved.
  13.  
  14. ------------------------------------------------------------------------------------------*/
  15.  
  16.  
  17. #ifndef __DOCUMENT__
  18. #define __DOCUMENT__
  19.  
  20. // Include necessary interface files
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24. #ifndef __QUICKDRAW__
  25. #include <QuickDraw.h>
  26. #endif
  27. #ifndef __WINDOWS__
  28. #include <Windows.h>
  29. #endif
  30. #ifndef __FILES__
  31. #include <Files.h>
  32. #endif
  33. #ifndef __STANDARDFILE__
  34. #include <StandardFile.h>
  35. #endif
  36.     /* canonical file specification */
  37.     struct CanonicalFileSpec
  38.     {
  39.         short        vRefNum;    /* volume reference number */
  40.         long        dirID;        /* directory ID */
  41.         Str63        fileName;    /* file name */
  42.     };
  43.  
  44. #ifndef __APPLICATION__
  45. #include "Application.h"
  46. #endif
  47. #ifndef __LIST__
  48. #include "List.h"
  49. #endif
  50.  
  51. // max and min inlines
  52. inline max(long a, long b)
  53. {
  54.     return a > b ? a : b;
  55. }
  56.  
  57. inline min(long a, long b)
  58. {
  59.     return a < b ? a : b;
  60. }
  61.  
  62. // Define inlines to convert between Points & Rects for convenience.
  63. inline Point TopLeft(Rect r)
  64. {
  65.     Point pt;
  66.  
  67.     pt.v = r.top;
  68.     pt.h = r.left;
  69.     return pt;
  70. }
  71.  
  72. inline Point TopRight(Rect r)
  73. {
  74.     Point pt;
  75.  
  76.     pt.v = r.top;
  77.     pt.h = r.right;
  78.     return pt;
  79. }
  80.  
  81. inline Point BotLeft(Rect r)
  82. {
  83.     Point pt;
  84.  
  85.     pt.v = r.bottom;
  86.     pt.h = r.left;
  87.     return pt;
  88. }
  89.  
  90. inline Point BotRight(Rect r)
  91. {
  92.     Point pt;
  93.  
  94.     pt.v = r.bottom;
  95.     pt.h = r.right;
  96.     return pt;
  97. }
  98.  
  99. const long kMaxSleepTime = 60;    // 1 second worth of ticks
  100.  
  101. // we derive from handle object to prevent fragmentation
  102. class TDocument : public HandleObject {
  103. protected:
  104.     WindowPtr            fDocWindow;
  105.     CanonicalFileSpec    fFile;
  106.     short                fFileRefNum;
  107.     OSType                fFileType;
  108.     Boolean                fNewDoc;
  109.     Boolean                fReadOnly;
  110.     Boolean                fDirty;
  111.     Boolean                fActive;
  112.  
  113.     // internal file i/o routines - these are the guys that do the
  114.     // actual work of manipulating files
  115.     virtual void        OpenFile(Boolean readOnly, Boolean createIfNecessary);
  116.         // OpenFile opens the file referred to by fFile. If createIfNecessary
  117.         // is true and the file doesn't exist, it will be created.
  118.         // If readOnly is true, the fReadOnly field will be set correctly
  119.         // and the file will be opened with a read-only access path.
  120.         // You shouldn't need to override this routine.
  121.     virtual void        CloseFile();
  122.         // close file just closes the file specified by fFileRefNum
  123.         // You shouldn't need to override this routine.
  124.     virtual void        ReadFromFile(short refNum) = 0;
  125.         // this routine reads the file's data from the data fork
  126.         // specified by refNum. The file will already be open,
  127.         // and in the correct position for reading.
  128.         // You MUST override this routine.
  129.     virtual void        WriteToFile(short refNum) = 0;
  130.         // this routine writes the files data to the data fork
  131.         // specified by refNum. The file will already be open,
  132.         // and in the correct position for writing.
  133.         // You MUST override this routine.
  134.  
  135.     virtual YNCResult    PresentSaveDialog(Boolean quitting);
  136.         // this routine puts up the standard Save dialog,
  137.         // using the filename specified by fFile. The quitting
  138.         // parameter is used to make sure we have the right
  139.         // phrasing in the dialog box - either
  140.         // 'Save "x" before quitting?' or 'Save "x" before closing?'
  141.  
  142. public:
  143.     // Constructor & Destructor
  144.                         TDocument(short resID, OSType theFileType);
  145.         // creates new, untitled document using resID as window template
  146.     virtual                ~TDocument();
  147.         // our destructor - disposes of window
  148.  
  149.     // Routines to handle basic user interface events
  150.     // you MUST override these in your subclasses!!!
  151.     virtual void        DoZoom(short partCode) = 0;
  152.     virtual void        DoGrow(EventRecord* theEvent) = 0;
  153.     virtual void        DoContent(EventRecord* theEvent) = 0;
  154.     virtual void        DoKeyDown(EventRecord* theEvent) = 0;
  155.     virtual void        DoUpdate() = 0;
  156.  
  157.     // called when activating/deactivating document.
  158.     // default version just sets fActive variable, so
  159.     // you will need to override this to hilite your selection
  160.     // and such
  161.     virtual void        DoActivate(Boolean becomingActive);
  162.  
  163.     // these routines are called after a TDocument object has been
  164.     // created to initialize its contents. You probably won't need to
  165.     // override these, since they are fairly generic.
  166.     virtual void        OpenNewDoc() {};
  167.         // OpenNewDoc sets up a new, untitled document - by default, this
  168.         // has already been done in the TDocument constructor, so this routine
  169.         // is just a placeholder.
  170.     virtual void        OpenOldDoc(CanonicalFileSpec theFile, Boolean readOnly);
  171.         // OpenOldDoc opens a document, reads its contents, sets the window
  172.         // title, and so on.
  173.  
  174.     // high level, user-oriented file handling routines
  175.     // You probably won't need to override these
  176.     virtual YNCResult    DoClose(Boolean askUserToSave,
  177.                                 YNCResult defaultAnswer,
  178.                                 Boolean quitting);
  179.         // DoClose closes the document. If askUserToSave is true,
  180.         // the user is asked whether or not he wants to save the documents
  181.         // contents. If it is false, the document will be saved if defaultAnswer
  182.         // is yesResult.
  183.     virtual void        DoSave();
  184.         // DoSave saves the documents contents. If the document is a new,
  185.         // untitled document, DoSave will call DoSaveAs (below). Otherwise,
  186.         // it sets up the file for writing and calls WriteToFile to save the
  187.         // actual data.
  188.     virtual void        DoSaveAs();
  189.         // DoSaveAs asks the user for a file to save the document into.
  190.         // It saves the data in the same manner as DoSave, and sets up
  191.         // the file-related data members (fFile, fFileRefNum, etc).
  192.     virtual void        DoRevert() {};
  193.         // DoRevert is a hook for a revert routine. Currently, this
  194.         // isn't implemented, but it is here for future enhancement.
  195.     virtual void        DoPrint() {};
  196.         // we don't support printing yet either
  197.  
  198.     // standard edit menu actions
  199.     // with the exception of DoUndo, you MUST override these routines
  200.     virtual void        DoUndo() {};
  201.         // by default, undo is unimplemented, so you don't have to
  202.         // override this method
  203.     virtual void        DoCut() = 0;
  204.     virtual void        DoCopy() = 0;
  205.     virtual void        DoPaste() = 0;
  206.     virtual void        DoClear() = 0;
  207.     virtual void        DoSelectAll() = 0;
  208.  
  209.     // idle time routines: you can override these to do cursor handling,
  210.     // TE caret blinking, marquee effects, etc...
  211.     virtual void        DoIdle() {};
  212.     virtual unsigned long CalcIdle() { return kMaxSleepTime; };
  213.         // we never need idle in typical applications, so return a very large number
  214.     virtual void        AdjustCursor() {};
  215.         // where is in local coords
  216.  
  217.     // query state of document - useful for adjusting menu state
  218.     // You will probably need to override at least a few of
  219.     // these to accurately reflect the state of your document
  220.     virtual Boolean        HaveUndo() { return false; };
  221.     virtual Boolean        HaveSelection() { return false; };
  222.     virtual Boolean        HavePaste() { return false; };
  223.     virtual Boolean        CanClose() { return (FrontWindow() != nil); };
  224.     virtual Boolean        CanSave() { return fDirty; };
  225.     virtual Boolean        CanSaveAs() { return true; };
  226.     virtual Boolean        CanRevert() { return false; };    // not implemented
  227.     virtual Boolean        CanPrint() { return false; };    // not implemented
  228.  
  229.     // utility routine to get window pointer for document
  230.     inline WindowPtr    GetDocWindow() { return fDocWindow; }
  231. };
  232.  
  233. // TDocumentList is a simple linked list of documents,
  234. // implemented C++ style.
  235. class TDocumentList : public TList {
  236. public:
  237.     virtual void        AddDoc(TDocument* doc);
  238.     virtual void        RemoveDoc(TDocument* doc);
  239.     // find the TDocument associated with the window
  240.     virtual TDocument*    FindDoc(WindowPtr window);
  241.     // return number of active documents
  242.     inline int            NumDocs() { return Count(); }
  243. };
  244.  
  245. #endif
  246.